package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Material;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liux on 2017/3/7.
 */

public class MaterialInListAdapter extends BaseAdapter {
    private List<Material> datas;
    private Context context;

    public MaterialInListAdapter(List<Material> datas, Context context) {
        this.context = context;
        this.datas = datas;
    }

    public void setDatas(List<Material> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_material, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Material material = datas.get(position);
        holder.orderRl.setVisibility(View.VISIBLE);
        holder.orderNoTv.setText(material.getOrderNo());
        if(Utils.isEmpty(material.getName())){
            holder.nameRl.setVisibility(View.GONE);
        }else{
            holder.name.setText(material.getName());
        }
        if(Utils.isEmpty(material.getManufacturer())){
            holder.manufacturerRl.setVisibility(View.GONE);
        }else{
            holder.manufacturer.setText(material.getManufacturer());
        }
        if(material.getQuantity() == 0){
            holder.numberRl.setVisibility(View.GONE);
        }else{
            holder.number.setText(material.getQuantity() + "");
        }
        if(Utils.isEmpty(material.getModel())){
            holder.modelRl.setVisibility(View.GONE);
        }else{
            holder.standard.setText(material.getModel());
        }
        holder.registerDate.setText(material.getApproachDate() + "  " + material.getCreateBy());
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.order_rl)
        RelativeLayout orderRl;
        @BindView(R.id.order_no_tv)
        HXTextView orderNoTv;
        @BindView(R.id.name_rl)
        RelativeLayout nameRl;
        @BindView(R.id.name)
        HXTextView name;
        @BindView(R.id.number_rl)
        RelativeLayout numberRl;
        @BindView(R.id.number)
        HXTextView number;
        @BindView(R.id.manufacturer_rl)
        RelativeLayout manufacturerRl;
        @BindView(R.id.manufacturer)
        HXTextView manufacturer;
        @BindView(R.id.model_rl)
        RelativeLayout modelRl;
        @BindView(R.id.standard)
        HXTextView standard;
        @BindView(R.id.register_date)
        HXTextView registerDate;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
